// ScriptEditor.cpp : Defines the entry point for the console application.
//
#include "ScriptEditor.h"
#include "ScriptLevel1.h"
#include "ScriptLevel2.h"
#include "Levels.h"

int _tmain(int argc, _TCHAR* argv[])
{
	IrrlichtDevice *device = createDevice(video::EDT_OPENGL, dimension2d<s32>(0,0), 16, false, false, false, 0);
		
	fpsLevelList(0,0);

	map<stringc, functionPointer>::Iterator levIt = levelList.getIterator();
	
	for(u32 i = 0 ; i < levelList.size(); levIt++ , i++)
	{
		functionList.clear();
		
		(levIt->getValue())(0,0);
		
		DEBUG_OUT("Exporting cinematics for %s \n" , levIt->getKey() ); 
		stringc path	="../../../data/game/";
		path			+=levIt->getKey();
		path			+="/cinematics/";

		map<stringc, functionPointer>::Iterator it = functionList.getIterator();

		for(u32 j = 0 ; j < functionList.size(); it++ , j++)
		{
			DEBUG_OUT("\tExporting script  %s \n" , it->getKey() ); 

			IXMLWriter* xml = device->getFileSystem()->createXMLWriter((path+(it->getKey())+".cff").c_str());

			if (!xml)
			{
				FAIL(xml , "could not create cff"); 
				return -1;
			}
		
			IAttributes* attr = device->getFileSystem()->createEmptyAttributes();

			xml->writeXMLHeader(); 
			xml->writeLineBreak();
		
			xml->writeComment(L"Cinematic file");	
			xml->writeLineBreak();
			xml->writeElement(L"cinematic", true);
			xml->writeLineBreak();

			(it->getValue())(xml , attr);	

			attr->drop();
			xml->writeClosingTag(L"cinematic");
		
			xml->drop();
		}
		
		functionList.clear();
	}

	device->closeDevice();
	
	DEBUG_OUT("Success!!!\n" ); 

	return 0;
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void writeCommand(IXMLWriter* xml , IAttributes* attr)
{
	xml->writeLineBreak();
	attr->write(xml, false);
	xml->writeClosingTag(L"command");
	xml->writeLineBreak();	
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _SetPosition( s32 objId , vector3df pos , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);
	attr->setAttribute("Position", pos );

	xml->writeElement(L"command", false, L"name", L"SetPosition");
	
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _SetRotation( s32 objId , quaternion rot , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);
	attr->setAttribute("Rotation", rot );

	xml->writeElement(L"command", false, L"name", L"SetRotation");
	
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _SetScale( s32 objId , vector3df pos , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);
	attr->setAttribute("Scale", pos );

	xml->writeElement(L"command", false, L"name", L"SetScale");
	
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _MoveToPosition( s32 objId, vector3df pos , f32 speed , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);	
	attr->setAttribute("Position", pos );
	attr->setAttribute("Speed", speed );

	xml->writeElement(L"command", false, L"name", L"MoveToPosition");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _MoveToObject( s32 objId , s32 destObj , f32 speed , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);
	attr->setAttribute("DestObj", destObj );
	attr->setAttribute("Speed", speed );

	xml->writeElement(L"command", false, L"name", L"MoveToEntity");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
//void _MoveToPositionWithSpeed( s32 objId , s32 speed , vector3df pos , IXMLWriter* xml , IAttributes* attr)
//{
//	attr->clear();
//		
//	attr->setAttribute("ObjectID" , objId);
//	attr->setAttribute("Speed", speed );
//	attr->setAttribute("Position", pos );
//
//	xml->writeElement(L"command", false, L"name", L"MoveToPositionWithSpeed");
//	writeCommand(xml,attr);
//}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
//void _MoveToEntityWithSpeed( s32 objId , s32 speed , s32 destObj , IXMLWriter* xml , IAttributes* attr)
//{
//	attr->clear();
//		
//	attr->setAttribute("ObjectID" , objId);
//	attr->setAttribute("Speed", speed );
//	attr->setAttribute("DestObj", destObj );
//
//	xml->writeElement(L"command", false, L"name", L"MoveToEntityWithSpeed");
//	writeCommand(xml,attr);
//}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _KillObject( s32 objId , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);
	
	xml->writeElement(L"command", false, L"name", L"KillObject");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _MoveEnemyToPosition( s32 objId , s32 state ,vector3df pos , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("EnemyID" , objId);
	attr->setAttribute("State", state );
	attr->setAttribute("Position", pos );

	xml->writeElement(L"command", false, L"name", L"MoveEnemyToPosition");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _MoveEnemyToObject( s32 objId , s32 state , s32 destObj , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("EnemyID" , objId);
	attr->setAttribute("State", state );
	attr->setAttribute("DestObj", destObj );

	xml->writeElement(L"command", false, L"name", L"MoveEnemyToObject");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _EnableAI( s32 objId ,IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);

	xml->writeElement(L"command", false, L"name", L"EnableAI");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _DisableAI( s32 objId ,IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);

	xml->writeElement(L"command", false, L"name", L"DisableAI");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _FreezeAI( s32 objId ,IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);

	xml->writeElement(L"command", false, L"name", L"DisableAI");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _StartEnemy( s32 objId ,IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);

	xml->writeElement(L"command", false, L"name", L"StartEnemy");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _StopEnemy( s32 objId ,IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);

	xml->writeElement(L"command", false, L"name", L"StopEnemy");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------

void _FollowWayPoint( s32 objId , s32 WayPointId , f32 speed , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
		
	attr->setAttribute("ObjectID" , objId);
	attr->setAttribute("WayPointID" , WayPointId);
	attr->setAttribute("Speed" , speed);

	xml->writeElement(L"command", false, L"name", L"FollowWayPoint");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _StopTest( IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
			
	xml->writeElement(L"command", false, L"name", L"StopTest");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _IfObjectInWayPoint( s32 ObjectId , s32 WayPointId , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
	attr->setAttribute("ObjectID" , ObjectId);
	attr->setAttribute("WayPointID" , WayPointId);
	xml->writeElement(L"command", false, L"name", L"IfObjectInWayPoint");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _IfObjectInPosition( s32 ObjectId , vector3df pos , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();
	attr->setAttribute("ObjectID" , ObjectId);
	attr->setAttribute("Position" , pos);
	xml->writeElement(L"command", false, L"name", L"IfObjectInPosition");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _IfAllEnemyDead( IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();	
	xml->writeElement(L"command", false, L"name", L"IfAllEnemyDead");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _IfEnemyDead(s32 EnemyID , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();	
	attr->setAttribute("EnemyID" , EnemyID);
	xml->writeElement(L"command", false, L"name", L"IfEnemyDead");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _EnableFog(IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	xml->writeElement(L"command", false, L"name", L"EnableFog");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _DisableFog(IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	xml->writeElement(L"command", false, L"name", L"DisableFog");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _SetupFog(s32 color , s32 transitionTime , s32 start , s32 end , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	attr->setAttribute("Color" , color);
	attr->setAttribute("TransitionTime" , transitionTime);
	attr->setAttribute("Start" , start);
	attr->setAttribute("End" , end);
	xml->writeElement(L"command", false, L"name", L"DisableFog");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _ResetFog(IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	xml->writeElement(L"command", false, L"name", L"ResetFog");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _EnableTrigger(s32 triggerID , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	attr->setAttribute("TriggerID" , triggerID);
	xml->writeElement(L"command", false, L"name", L"EnableTrigger");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _DisableTrigger(s32 triggerID , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	attr->setAttribute("TriggerID" , triggerID);
	xml->writeElement(L"command", false, L"name", L"DisableTrigger");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _OpenDoor(s32 doorID , bool keepOpen , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	attr->setAttribute("DoorID" , doorID);
	attr->setAttribute("KeepOpen" , keepOpen);
	xml->writeElement(L"command", false, L"name", L"OpenDoor");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _CloseDoor(s32 doorID , bool keepClosed , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	attr->setAttribute("DoorID" , doorID);
	attr->setAttribute("KeepClosed" , keepClosed);
	xml->writeElement(L"command", false, L"name", L"CloseDoor");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _Delay(s32 timeMS , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	attr->setAttribute("Time" , timeMS);
	xml->writeElement(L"command", false, L"name", L"Delay");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _SetEnemyState(s32 EnemyID , s32 state , IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	attr->setAttribute("EnemyID" , EnemyID);
	attr->setAttribute("State" , state);
	xml->writeElement(L"command", false, L"name", L"SetEnemyState");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _EndLevel(IXMLWriter* xml , IAttributes* attr)
{
	attr->clear();		
	xml->writeElement(L"command", false, L"name", L"EndLevel");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
void _SetAnim(s32 objectId , s32 anim , IXMLWriter* xml , IAttributes*attr)
{
	attr->clear();	
	attr->setAttribute("ObjectID" , objectId);
	attr->setAttribute("Anim" , anim);
	xml->writeElement(L"command", false, L"name", L"SetAnim");
	writeCommand(xml,attr);
}
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
